package com.yuedou.jylt.utils;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;


/**
 * 主要用来做一下初始化的操作
 */
public abstract class ProApplication extends MultiDexApplication {

    protected static ProApplication instance;

    protected static Context mContext;

    protected static String FILE_PROVIDER = "cn.dxframe.FILE_PROVIDER";

    //声明一个静态常量，用作退出MainActivity的Tag
    public static final String EXIST = "exist";

    public static String APPLICATION_ID;

    public static boolean isDebug = false;

    private static int count_activity = 0;


    /**
     * handle Modules
     */
    protected abstract void handleModules();

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;
        mContext = this;

        handleModules();

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
                count_activity++;
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
                if (count_activity > 0) {
                    count_activity--;
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }

    public static ProApplication getInstance() {
        return instance;
    }

    /**
     * @return 全局的上下文
     */
    public static Context getmContext() {
        return mContext;
    }

    /**
     * getFileProvider
     *
     * @return
     */
    public static String getFileProvider() {
        return FILE_PROVIDER;
    }

    /**
     * 判断app是否在后台
     *
     * @return
     */
    public static boolean isBackground() {
        if (count_activity <= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static int getCount_activity() {
        return count_activity;
    }
}
